package com.tianji.learning.mq;

import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.domain.dto.LikedTimesDTO;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuchun
 * @version 1.0
 * @date 2024/9/5 14:19
 */
@Component
@Slf4j
public class LikedRecordListener {

    @Autowired
    private IInteractionReplyService replyService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "qa.liked.times.queue",durable = "true"),
                    exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE,type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.QA_LIKED_TIMES_KEY))
    public void likedRecordListener(List<LikedTimesDTO> likedTimesDTOS){
        log.debug("LikedRecordListener 监听到消息: {}",likedTimesDTOS);
        List<InteractionReply> list = new ArrayList<>(likedTimesDTOS.size());
       for (LikedTimesDTO dto : likedTimesDTOS) {
           InteractionReply reply = new InteractionReply();
           reply.setId(dto.getBizId());
           reply.setLikedTimes(dto.getLikedTimes());
           list.add(reply);
       }
       //批量更新
       replyService.updateBatchById(list);
    }
}
